package com.rd.student.dashuju.wangbo.demo4;

import java.util.concurrent.locks.ReentrantLock;

public class TestBank03 implements Runnable{
    static Account account;
    static Double money;


    public TestBank03( Account account, Double money) {
        this.account = account;
        this.money = money;

    }
    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        lock.lock();

        if (account.balance < money) {
            System.out.println("余额不足");
            return;
        }

        System.out.println(Thread.currentThread().getName() + "可以取钱");
        account.balance = account.balance - money;
        System.out.println("取钱后的余额为：" + account.balance);
        lock.unlock();
    }


    public static void main(String[] args) {
        Account account = new Account("123456", 888);
        Runnable runnable1 = new TestBank03(account, 666.6);
        Runnable runnable2 = new TestBank03(account, 666.6);
        Thread thread1 = new Thread(runnable1,"zhangsan");
        Thread thread2 = new Thread(runnable2,"lisi");
        thread1.start();
        thread2.start();

    }
}
